package com.lanhai.zhonglai.utils;

import java.util.ArrayList;
import java.util.List;
 

public class Tools {
	public static String byte2HexStr(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs += " 0" + stmp;
			else
				hs += " " + stmp;  
		}
		return hs.toUpperCase().substring(1);
	}
	
	public static short toIntL(byte[] b) {
		short res = 0;
		for (int i = 0; i < b.length; i++) {
			res += (b[i] & 0xff) << (i * 8);
		}
		return res;
	}
	
	public static byte[] toLH(int n) {  
		  byte[] b = new byte[4];  
		  b[0] = (byte) (n & 0xff);  
		  b[1] = (byte) (n >> 8 & 0xff);  
		  b[2] = (byte) (n >> 16 & 0xff);  
		  b[3] = (byte) (n >> 24 & 0xff);  
		  return b;  
		}
	
	
	public static short toIntH(byte[] b){
		short res = 0;
	    for(int i=0;i<b.length;i++){
	        res += (b[i] & 0xff) << ((3-i)*8);
	    }
	    return res;
	}
	
	public static long strToLong(String s) {
		try {
			return Long.parseLong(s);
		}
		catch(Exception e) {
			return 0;
		}
	}
	
	public static int strToInt(String s) {
		try {
			return Integer.parseInt(s);
		}
		catch(Exception e) {
			return 0;
		}
	}
	
	public static List<Double> strToDoubleList(String s) {
		List<Double> list=new ArrayList<Double>();
		if(s==null||s.equals("")) return list;
		
		String[] tmp=s.split(",");
		for(String t : tmp) {
			if(t.equals("")) continue;
			Double m=0.0;
			try {
				m= Double.parseDouble(s);
			}
			catch(Exception e) { 
			}
			list.add(m);
		}
	
		return list;
	}
	
	/**
	 * 计算agv离开交管的距离
	 * @param agvx
	 * @param agvy
	 * @param areax
	 * @param areay
	 * @return
	 */
	public static boolean farawayArea(int agvx,int agvy,int areax,int areay) {
		int dix=agvx-areax;
		int diy=agvy-areay;
		double dist=Math.sqrt(dix*dix+diy*diy);
		if(dist>=200) { //200cm=2米
			return true;
		}else {
			return false;
		}
	}
	
 
	  
	 
	
	
	 public static byte[] getBooleanArray(byte b) {  
	        byte[] array = new byte[8];  
	        for (int i = 7; i >= 0; i--) {  
	            array[i] = (byte)(b & 1);  
	            b = (byte) (b >> 1);  
	        }  
	        return array;  
	    }  
}
